const db = require('../db/db');

//注册新用户处理函数，post用户数组申请id，传输所有值
exports.regUser = (req,res) => {
    const data = req.query;
    const sqlstr = 'insert into users set ?';
    var id;
    db.query(sqlstr,data,(err,result)=>{
        if(err) return console.log(err.message);
        if(result.affectedRows===1){
            id=result.insertId;
            res.send('新用户添加成功,id为'+id);
            console.log('new user add');
        }
    })

}
//登录处理函数,通过id登录
exports.login =(req,res)=>{
    const id = req.query.id;
    const sqlstr = 'select * from users where id =? and status =0';
    db.query(sqlstr,id,(err,result)=>{
        if(err) return console.log(err.message);
        res.send(JSON.parse(JSON.stringify(result))[0]);
        console.log('成功发送')
        //[{"id":28,"name":"zhuce","work":" ","num":" ","wx":" ","mail":" ","region":" ","place":" ","about":" ","status":0,"img":""}]
    })
    // var regions=getData.region.split('"');
    //     var arr1=['','',''];
    //     for(var i =0;i<=arr1.length;i++){
    //         arr1[i]=regions[2*i+1]
    //     }
    //     getData.region=arr1;
    //     // res.send(getData);
    //     console.log(getData)
    //     console.log('成功发送用户信息');
}

//查找用户（个人）
exports.search_p =(req,res)=>{
    const sqlstr = `select * from users where name =? and region =? and id!= ? and status =0`;
    const data = req.query;
    db.query(sqlstr,[data.name,data.region,data.id],(err,result)=>{
        if(err) return console.log(err.message);
        res.send(JSON.parse(JSON.stringify(result)))
        console.log(JSON.parse(JSON.stringify(result)))
    }) 
    //[{"id":5,"name":"123","work":" ","num":" ","wx":" ","mail":" ","region":" ","place":" ","about":" ","status":0,"img":""}]
    
    
}

//公司成员展示，上传公司名搜索
exports.show_com =(req,res)=>{
    const com = req.query.place;
    const sqlstr = 'select * from users where place =?  and status =0';
    db.query(sqlstr,com,(err,result)=>{
        if(err) return console.log(err.message);
        res.send(JSON.parse(JSON.stringify(result)))
    }) 
    //[{"id":31,"name":"123778","work":"ui设计师","num":"11111111111","wx":"11111111111","mail":"1111111111@qq.com","region":"河北省，石家庄市，裕华区","place":"ceshi","about":"ceshi","status":0,"img":"ceshi"}]
}


//修改个人信息
exports.change = (req,res) => {
    const data = req.query;
    const sqlstr = 'update users set ? where id =?';
    db.query(sqlstr,[data,data.id],(err,result)=>{
        if(err) return console.log(err.message);
        if(result.affectedRows===1){
            res.send('用户信息修改成功');
        }
    })
    
    
}


//后台查找用户
exports.search_p_fromh =(req,res)=>{
    const sqlstr = `select * from users where name =?`;
    const data = req.query.name;
    db.query(sqlstr,data,(err,result)=>{
        if(err) return console.log(err.message);
        res.send(JSON.parse(JSON.stringify(result)))
        console.log('ok,search_p')
    }) 
    //[{"id":5,"name":"123","work":" ","num":" ","wx":" ","mail":" ","region":" ","place":" ","about":" ","status":0,"img":""}]
    
    
}

//后台修改用户状态
exports.change_p = (req,res) => {
    const data = req.query;
    const sqlstr = 'update users set status=? where id =?';
    db.query(sqlstr,[data.status,data.id],(err,result)=>{
        if(err) return console.log(err.message);
        if(result.affectedRows===1){
            res.send('用户信息修改成功');
        }
    })
    console.log(data)
    
}


//后台获取所有用户
exports.all_p =(req,res)=>{
    const sqlstr = `select * from users`;
    db.query(sqlstr,(err,result)=>{
        if(err) return console.log(err.message);
        res.send(JSON.parse(JSON.stringify(result)))
        console.log('ok,all_p')
    }) 
    //[{"id":5,"name":"123","work":" ","num":" ","wx":" ","mail":" ","region":" ","place":" ","about":" ","status":0,"img":""}]
    
    
}